
import Vue from 'vue'
import store from '../store/index.js'
import Router from 'vue-router'

import NavArr from '../nav-config'

import Layout from '../components/Layout'

import login from '../views/login'
import index from '../views/index'

import resetPassword from '../views/resetPassword'

import productList from '../views/product/product-list'
import productEdit from '../views/product/product-edit'
import productDetail from '../views/product/product-detail'

import managerList from '../views/manager/manager-list'
import managerEdit from '../views/manager/manager-edit'
import managerDetail from '../views/manager/manager-detail'

import promoList from '../views/promo/promo-list'
import promoEdit from '../views/promo/promo-edit'
import promoDetail from '../views/promo/promo-detail'

import orderList from '../views/order/order-list'
import orderDetail from '../views/order/order-detail'

import managerApplyList from '../views/managerApply/managerApply-list'
import managerApplyDetail from '../views/managerApply/managerApply-detail'

Vue.use(Router);
const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: login
    },
    {
      path: '/index',
      component: Layout,
      redirect: '/index/welcome',
      children: [
        {
          path: '/index/welcome',
          component: index,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/resetPassword',
          component: resetPassword,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/product/list',
          component: productList,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/product/edit',
          component: productEdit,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/product/detail',
          component: productDetail,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/manager/list',
          component: managerList,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/manager/edit',
          component: managerEdit,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/manager/detail',
          component: managerDetail,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/promo/list',
          component: promoList,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/promo/edit',
          component: promoEdit,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/promo/detail',
          component: promoDetail,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/order/list',
          component: orderList,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/order/detail',
          component: orderDetail,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/managerApply/list',
          component: managerApplyList,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: '/managerApply/detail',
          component: managerApplyDetail,
          meta: {
            requiresAuth: true
          }
        }

      ]
    },
  ]
});


//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {

  //将解析的导航数组挂在Vue上给页面调用
  //通过地址栏的路由来解析
  let goto = to.fullPath;
  Vue.prototype.$menuArr = [];
  NavArr.forEach(nav => {
    if (nav.index == goto) {
      Vue.prototype.$menuArr.push(nav.title);
      return;
    }
    if (nav.subs) {
      nav.subs.forEach(sub => {
        if (sub.index == goto) {
          Vue.prototype.$menuArr.push(nav.title);
          Vue.prototype.$menuArr.push(sub.title);
          return;
        }
      })
    }

  })

  //获取store里面的token
  let token = store.state.token;
  //判断要去的路由有没有requiresAuth
  if (to.meta.requiresAuth) {

    if (token) {
      next();
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
      });
    }

  } else {
    next();//如果无需token,那么随它去吧
  }
});

export default router;
